/*
 * @Author: asushiye
 * @Date: 2020-07-02 15:30:23
 * @Commit: file content
 */
import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { AfMessageService } from '@core';
import { STColumn, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { AfOperType } from 'src/app/core/af/af-const';
import { AfExcelService, ExcelUploadRecord } from 'src/app/routes/system/excel/af-excel.service';
import { ExcelUploadComponent } from 'src/app/routes/system/excel/upload/excel-upload.component';

import { DataplatformService } from '../dataplatform.service';

@Component({
  selector: 'app-cg-dp-term',
  templateUrl: './dp-term.component.html'
})
export class DataplatformTermComponent implements OnInit {
  rows: any[] = [];
  queryParams: any = { termEn: '', termCn: '', termType: '' };
  url = '';
  req = { params: this.queryParams, lazyLoad: false };

  isLoading = false;
  isExport = false;
  isImport = false;
  afOperType = AfOperType.add;
  databases: any[] = [];

  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [
    { title: '术语', index: 'termCn' },
    { title: '术语描述', index: 'termCnDesc' },
    { title: '英文', index: 'termEn' },
    { title: '英文全称', index: 'termEnDesc' },
    { title: '术语类型', index: 'termType' },
    // { title: '备注', index: 'termRemark' },
    // { title: '设计人', index: 'termDesigner' },
    // { title: '创建人', index: 'createdBy' },
    // { title: '创建时间', index: 'createdDate' },
    { title: '更新人', index: 'lastModifiedBy' },
    { title: '更新时间', index: 'lastModifiedDate' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          tooltip: `手动测试链接`,
          click: _record => {
            this.edit(_record);
          }
        },
        {
          text: '删除',
          icon: 'delete',
          type: 'del',
          tooltip: `删除此记录`,
          pop: {
            title: '您确定要删除此记录吗？',
            okType: 'danger',
            icon: 'star'
          },
          click: (record, _modal) => {
            this.delete(record);
          }
        }
      ]
    }
  ];

  constructor(
    private cdr: ChangeDetectorRef,
    public msg: AfMessageService,
    private dpSrv: DataplatformService,
    private modal: ModalHelper,
    private excelSrv: AfExcelService
  ) {
    this.url = this.dpSrv.getTermsUrl();
  }
  ngOnInit() {
    this.query();
  }

  public query() {
    // console.log(JSON.stringify(this.queryParams));
    this.st?.reset(this.queryParams);
  }

  delete(item: any) {
    try {
      this.dpSrv.deleteTerm(item.termCn).subscribe(body => {
        if (body.status) {
          this.rows = [];
          this.msg.notice_success('删除记录', `${item.termCn} 记录删除成功`);
          this.query();
        } else {
          this.msg.notice_warning('删除记录', `${item.termCn} 删除失败，警告编号${body.code};  警告信息${body.msg}`);
        }
      });
    } finally {
      setTimeout(() => {
        this.cdr.detectChanges();
      }, 200);
    }
  }

  tForm = new FormGroup({
    termCn: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    termCnDesc: new FormControl('', {}),
    termEn: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    termEnDesc: new FormControl(''),
    termType: new FormControl('', {}),
    termRemark: new FormControl('', {}),
    termDesigner: new FormControl('', {})
  });

  drawerOption: any = { size: 'large', visible: false, title: '编辑' };
  openDrawer() {
    this.drawerOption.visible = true;
    if (this.afOperType == AfOperType.edit) {
      this.tForm.controls.termCn.disable();
    } else {
      this.tForm.controls.termCn.enable();
    }
  }

  closeDrawer() {
    this.drawerOption.visible = false;
  }

  save() {
    if (this.tForm.invalid) {
      this.msg.notice_warning('警告', '表单无效不能保存');
      return;
    }
    this.dpSrv.saveTerm(this.tForm.getRawValue()).subscribe(res => {
      if (res.status) {
        this.query();
        this.msg.notice_success('', '保存成功');
        this.closeDrawer();
      } else {
        this.msg.notice_warning('保存失败', res.msg);
      }
    });
  }

  autoTips: Record<string, Record<string, string>> = {
    'zh-cn': {
      required: '必填项'
    },
    en: {
      required: 'Input is required'
    }
  };

  add() {
    this.afOperType = AfOperType.add;
    this.tForm.reset();
    this.openDrawer();
  }

  edit(item: any) {
    this.afOperType = AfOperType.edit;
    this.tForm.reset();
    this.tForm.patchValue(item);
    this.openDrawer();
  }
}
